//
//  HMWelcomeViewController.swift
//  HMWeibo12
//
//  Created by iOS on 16/9/19.
//  Copyright © 2016年 iOS. All rights reserved.
//

import UIKit
import SDWebImage

/// 欢迎界面
class HMWelcomeViewController: UIViewController {
    
    // 头像底部约束
    var iconViewBottomCons: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        // 使用本地用户信息的头像先显示
        if let avatar_large = HMUserAccountVM.shared.userAccount?.avatar_large {
            let url = URL(string: avatar_large)
            iconView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        // 获取用户信息
        HMUserAccountVM.shared.loadUserInfo { (error: Error?) in
            if error != nil {
                print("加载用户信息失败")
                return
            }
            
            // 加载用户信息成功了,修改头像
            // 获取用户头像地址
            if let avatar_large = HMUserAccountVM.shared.userAccount?.avatar_large {
                // 使用SDWebImage显示头像
                let url = URL(string: avatar_large)
                self.iconView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
        
        // 这个地方,子控件都还没有正确的frame,一做动画,大家都动画
//        startAnimation()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    private func setupUI() {
        // 添加子控件
        self.view.addSubview(backgroundImageView)
        self.view.addSubview(iconView)
        self.view.addSubview(messageLabel)
        
        // 添加约束, OC -> Masonry, Swift -> SnapKit
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景, 填充父控件 -> 4边和父控件重合
        // leading
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0))
        // top
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0))
        // trailing
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0))
        // bottom
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0))
        
        // 头像
        // centerX
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0))
        
        iconViewBottomCons = NSLayoutConstraint(item: iconView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -160)
        // y
        self.view.addConstraint(iconViewBottomCons!)
        // width
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 85))
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 85))
        
        // 文字
        // centerX
        self.view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0))
        // y
        self.view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1, constant: 16))
    }
    
    // 头像动画
    private func startAnimation() {
        // 修改底部约束
        iconViewBottomCons?.constant = -(UIScreen.height - 160)
        
        // usingSpringWithDamping: 弹簧明显程度 0 - 1.0之间
        // initialSpringVelocity: 初始速度
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { 
            // 动画
            // layoutIfNeeded 根据当前新的约束重新计算frame值
            self.view.layoutIfNeeded()
            }) { (_) in
                // 弹簧动画完成
                print("弹簧动画完成")
                
                // 让Label渐变出来
                UIView.animate(withDuration: 0.25, animations: { 
                    self.messageLabel.alpha = 1
                    }, completion: { (_) in
                        // 切换到 HMMainTabbarController
                        UIApplication.shared.keyWindow?.rootViewController = HMMainTabBarController()
                })
        }
    }
    
    // MARK: - 懒加载
    /// 背景
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 切圆
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    /// 文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.text = "欢迎归来"
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = UIColor.black
        
        label.alpha = 0
        
        return label
    }()
}
